<template>
  <div class="app-container">
    <el-row :gutter="20">
      <!--活动数据-->
      <el-col :span="4" :xs="24">
        <div class="head-container">
          <el-input
            v-model="eventName"
            placeholder="请输入活动名称"
            clearable
            size="small"
            prefix-icon="el-icon-search"
            style="margin-bottom: 20px"
          />
        </div>
        <div class="head-container">
          <el-tree
            :data="eventOptions"
            :props="defaultProps"
            :expand-on-click-node="false"
            :filter-node-method="filterNode"
            ref="tree"
            @node-click="handleNodeClick"
          />
        </div>
      </el-col>
    <!--活动数据-->
    <el-col :span="20" :xs="24">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="志愿者姓名"  prop="sysUsers.nickName" label-width="95px">
        <el-input
          v-model="queryParams.sysUsers.nickName"
          placeholder="请输入志愿者姓名"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item label="签到状态" prop="statusSignUp">
        <el-select
          v-model="queryParams.statusSignUp"
          placeholder="请选择签到状态"
          clearable
          size="small"
          style="width: 180px"
        >
          <el-option
            v-for="dict in dict.type.sys_status_sign_up"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>

        <el-form-item label="签退状态" prop="statusSignOut">
          <el-select
            v-model="queryParams.statusSignOut"
            placeholder="请选择签退状态"
            clearable
            size="small"
            style="width: 180px"
          >
            <el-option
              v-for="dict in dict.type.sys_status_sign_out"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            />
          </el-select>
      </el-form-item>
      <el-form-item label="审核状态" prop="isCheck">
        <el-select
          v-model="queryParams.isCheck"
          placeholder="请选择审核状态"
          clearable
          size="small"
          style="width: 180px"
        >
          <el-option
            v-for="dict in dict.type.sys_check"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>

      <el-form-item label="签到时间" prop="timeSignUp" >
        <el-date-picker clearable size="small"
          v-model="queryParams.timeSignUp"
          type="datetime"
          value-format="yyyy-MM-dd HH:mm"
          placeholder="选择签到时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="签退时间" prop="timeSignOut">
        <el-date-picker clearable size="small"
          v-model="queryParams.timeSignOut"
          type="datetime"
          value-format="yyyy-MM-dd HH:mm"
          placeholder="选择签退时间">
        </el-date-picker>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['volunteer:user:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['volunteer:user:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['volunteer:user:del']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['volunteer:user:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="eventList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="志愿者姓名" align="center" prop="sysUsers.nickName" />
      <el-table-column label="志愿活动名称" align="center" prop="sysEvent.eventName" />
      <el-table-column label="签到时间" align="center" prop="timeSignUp" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.timeSignUp, '{y}-{m}-{d} {h}:{i}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="签退时间" align="center" prop="timeSignOut" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.timeSignOut, '{y}-{m}-{d} {h}:{i}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="服务时长" align="center" prop="longtime" />
      <el-table-column label="签到状态" align="center" prop="statusSignUp">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_status_sign_up" :value="scope.row.statusSignUp"/>
        </template>
      </el-table-column>
      <el-table-column label="签退状态" align="center" prop="statusSignOut">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_status_sign_out" :value="scope.row.statusSignOut"/>
        </template>
      </el-table-column>
      <el-table-column label="审核状态" align="center" prop="isCheck">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_check" :value="scope.row.isCheck"/>
        </template>
      </el-table-column>

      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="180">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-circle-check"
            @click="handlesignUp(scope.row)"
            v-hasPermi="['volunteer:user:start']"
          >签到</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-circle-check"
            @click="handlsignOut(scope.row)"
            v-hasPermi="['volunteer:user:end']"
          >签退</el-button>

          <el-dropdown size="mini" @command="(command) => handleCommand(command, scope.row)" v-hasPermi="['system:role:edit']">
            <span class="el-dropdown-link">
              <i class="el-icon-d-arrow-right el-icon--right"></i>更多
            </span>
            <el-dropdown-menu slot="dropdown">
              <el-button
                size="mini"
                type="text"
                icon="el-icon-delete"
                @click="handleDelete(scope.row)"
                v-hasPermi="['volunteer:user:remove']"
              >取消报名</el-button>
              <el-button
                size="mini"
                type="text"
                icon="el-icon-edit"
                @click="checkVounteer(scope.row)"
                v-hasPermi="['volunteer:user:check']"
              >志愿者审核</el-button>
              <el-upload
                class="upload-demo"
                action="http://localhost/dev-api/volunteer/image/uploadUserEvent"
                :data="{eventId:scope.row.eventId,userId:1}"
                :headers="headers"
                show-file-list="true"
                :limit="1"
              >
                <el-button
                  size="mini"
                  type="text"
                  icon="el-icon-plus"
                >上传图片</el-button>
              </el-upload  >
            </el-dropdown-menu>
          </el-dropdown>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
    </el-col>
  </el-row>
    <!-- 添加或修改报名签到签退对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="用户id" prop="userId">
          <el-input v-model="form.userId" placeholder="请输入用户id" />
        </el-form-item>
        <el-form-item label="志愿活动id" prop="eventId">
          <el-input v-model="form.eventId" placeholder="请输入志愿活动id" />
        </el-form-item>
        <el-form-item label="签到状态" prop="statusSignUp">
          <el-input v-model="form.statusSignUp" placeholder="请输入签到状态" />
        </el-form-item>
        <el-form-item label="签退状态" prop="statusSignOut">
          <el-input v-model="form.statusSignOut" placeholder="请输入状态" />
        </el-form-item>
        <el-form-item label="签到时间" prop="timeSignUp">
          <el-date-picker clearable size="small"
            v-model="form.timeSignUp"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="选择签到时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="签退时间" prop="timeSignOut">
          <el-date-picker clearable size="small"
            v-model="form.timeSignOut"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="选择签退时间">
          </el-date-picker>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

<!--    志愿者审核页面-->
    <el-dialog  title="详细信息" :visible.sync="openView" width="600px" :data="eventList"  append-to-body>
        <el-form ref="form" :model="form" label-width="120px" size="mini">
          <el-row>
            <el-col :span="12">
              <el-form-item v-if="form.sysUsers!=undefined" label="志愿者姓名:">{{ form.sysUsers.nickName}}</el-form-item>
              <el-form-item label="志愿图片:">
                <el-image style="width: 100%;height:100%" fit="scale-down" :initial-index="0" :src="form.img" ></el-image>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="签到时间:">{{ form.timeSignOut}}</el-form-item>
              <el-form-item label="签退时间:">{{form.timeSignUp}}</el-form-item>
            </el-col>

          </el-row>
        </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="checkOk(form)">审核</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  addUserEvent, check_fail, check_ok, delReport,
  delUserEvent,
  getUserEvent,
  listUserEvent, listUsersEvent, searchUserEvent, signOut,
  signUp, treeEventSeqlect,
  updateUserEvent
} from "../../../api/volunteer/user";
import {treeEventSelect} from "../../../api/volunteer/event";
import {listUser} from "@/api/system/user";
import {getToken} from "@/utils/auth";

export default {
  name: "Event",
  dicts: ['sys_status_sign_up','sys_check','sys_status_sign_out'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 报名签到签退表格数据
      eventList: [],
      // 弹出层标题
      title: "",
      //是否显示详细弹出层
      openView:false,
      //活动名称
      eventName:undefined,
      // 活动树选项
      eventOptions: undefined,
      // 活动树名称
      // 是否显示弹出层
      open: false,
      headers:{
        Authorization:"Bearer" + ' ' +getToken()
      },
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        userId: null,
        eventId: null,
        statusSignUp: null,
        statusSignOut: null,
        timeSignUp: null,
        timeSignOut: null,
        longtime: null,
        img: null,
        isCheck: null,
         sysUsers:{
          nickName:null
      },
        sysEvent:{
          eventTypeId:null
        }


      },
      // 表单参数
      form: {},
      defaultProps: {
        children: "children",
        label: "label"
      },
      // 表单校验
      rules: {
        userId: [
          { required: true, message: "用户id不能为空", trigger: "blur" }
        ],
        eventId: [
          { required: true, message: "志愿活动id不能为空", trigger: "blur" }
        ],
      }
    };
  },
  watch:{
    // 根据名称筛选部门树
    eventName(val) {
      this.$refs.tree.filter(val);
    }
  },
  created() {
    this.getList();
    this.getTreeselect();
  },
  methods: {

    /** 查询报名签到签退列表 */
    getList() {
      this.loading = true;
      listUsersEvent(this.queryParams).then(response => {
        console.log('挂载时加载的数据',response)
        this.eventList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },

    /** 查询部门下拉树结构 */
    getTreeselect() {
      treeEventSeqlect().then(response => {
        this.eventOptions= response.data;
      });
    },
    // 筛选节点
    filterNode(value, data) {
      if (!value) return true;
      return data.label.indexOf(value) !== -1;
    },
    // 节点单击事件
    handleNodeClick(data) {
      console.log('data',data)
      console.log('data.id',data.id)
      if(data.children){
        this.queryParams.sysEvent.eventTypeId=data.id
      }else {
        this.queryParams.eventId = data.id;
      }
      this.handleQuery();
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        userId: null,
        eventId: null,
        statusSignUp: null,
        statusSignOut: null,
        timeSignUp: null,
        timeSignOut: null,
        longtime: null,
        img: null,
        isCheck: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      console.log('query',this.queryParams)
      console.log('nickName',this.queryParams.sysUsers.nickName)
      this.queryParams.pageNum = 1;
      this.getList();
      console.log('跑没跑')
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.userId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加报名签到签退";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const userId = row.userId || this.ids
      getUserEvent(userId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改报名签到签退";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.userId != null) {
            updateUserEvent(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addUserEvent(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 取消报名按钮操作 */
    handleDelete(row) {
      this.$modal.confirm('是否确认删除报名签到签退编号为"' + row.userId + '"的数据项？').then(function() {
        return delReport(row.userId,row.eventId);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('volunteer/event/export', {
        ...this.queryParams
      }, `event_${new Date().getTime()}.xlsx`)
    },
    //签到
    handlesignUp(row){
      this.$modal.confirm('是否确认签到？').then(function() {
        console.log('row',row)
        console.log('名字',row.sysUsers.nickName)
        return signUp(row.userId, row.eventId);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("签到成功!");
      }).catch(() => {});
    },
    //签退
    handlsignOut(row){
      this.$modal.confirm('是否确认签退？').then(function() {
        return signOut(row.userId, row.eventId);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("签退成功!");
      }).catch(() => {});
    },
    //审核志愿者
    checkVounteer(row){
      this.openView=true
      //构造请求
      const req={
        userId:row.userId,
        eventId:row.eventId
      }
      listUsersEvent(req).then((response)=>{
        console.log('rows',response.rows)

      this.form=response.rows[0]
        console.log('form',this.form)
        console.log('this.form',this.form.sysUsers.nickName)
      })
    },
    //审核不通过志愿者
    checkFalse(row){

    },

    //审核通过志愿者
    checkOk(row){
      this.$confirm('是否审核通过志愿者'+row.sysUsers.userName+'?','提示',{
        confirmButtonText:'是',
        cancelButtonText:'否',
        type:'warning'
      }).then(()=>{
        check_ok(row.userId,row.eventId).then(response => {
          this.$modal.msgSuccess("审核通过!");
          this.getList();
        }
        )
        this.openView=false
      }).catch(()=>{
          check_fail(row.userId,row.eventId).then((response)=>{
            this.getList();
            this.$modal.msgError("审核不通过")
          })
          this.openView=false

      })

    }

  }
};
</script>
